package com.bls.cate.ui.activity.search_user;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bls.blslib.base.BaseActivity;
import com.bls.cate.R;
import com.bls.cate.adapter.SearchUserListAdapter;
import com.bls.cate.constant.Constant;
import com.bls.cate.constant.Interface;
import com.bls.cate.listener.EditTextChangedListener;
import com.bls.cate.listener.EditorActionListener;
import com.bls.cate.model.SearchUserRes;
import com.bls.cate.ui.widget.TitleView;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.util.ArrayList;
import java.util.List;

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2018/1/10.
 */

public class SearchUserActivity extends BaseActivity implements EditTextChangedListener.SearchUserListener, EditorActionListener.SearchUserListener, TitleView.OnTitleClickListener {

    private SearchUserActivity mActivity;
    private TitleView titleView;
    private TextView btnCancelSearch;
    private LinearLayout btnSearch;
    private EditText etSearch;
    private RecyclerView rvList;

    private String uniqueId;
    private List<SearchUserRes.ResultListDataBean> resultListDataBeans;
    private EditorActionListener editorActionListener;
    private SearchUserListAdapter searchUserListAdapter;


    @Override
    protected void initParams() {
        super.initParams();
        uniqueId = getIntent().getStringExtra("UniqueId");
        mActivity = (SearchUserActivity) getActivity();
        resultListDataBeans = new ArrayList<>();
        searchUserListAdapter = new SearchUserListAdapter(resultListDataBeans);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_user;
    }

    @Override
    protected void initFvb() {
        super.initFvb();
        titleView = findViewById(R.id.title_view);
        etSearch = findViewById(R.id.et_search);
        btnCancelSearch = findViewById(R.id.btn_cancel_search);
        btnSearch = findViewById(R.id.btn_search);
        rvList = findViewById(R.id.rv_list);
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        titleView.setTitleMiddleText("寻找好友");
        titleView.setBtnMode(1, 0);
        titleView.setTitleClickListener(this);
    }

    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rvList.setLayoutManager(layoutManager);
        rvList.setHasFixedSize(true);
        rvList.setAdapter(searchUserListAdapter);
    }

    @Override
    protected void initListener() {
        setOnClickListener(btnCancelSearch, btnSearch);
        EditTextChangedListener editTextChangedListener = new EditTextChangedListener(mActivity, this);
        editorActionListener = new EditorActionListener(mActivity, "", this);
        etSearch.addTextChangedListener(editTextChangedListener);
        etSearch.setOnEditorActionListener(editorActionListener);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel_search:
                etSearch.setText("");
                btnSearch.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_search:
                btnSearch.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void searchUserResult(String s) {
        editorActionListener.setStrInput(s);
    }

    @Override
    public void searchResult(Constant.SearchStateEnum searchStateEnum, String s) {
        int searchType = searchStateEnum == Constant.SearchStateEnum.NICKNAME ? 0 : (searchStateEnum == Constant.SearchStateEnum.NICKNAME_PHONE ? 1 : (searchStateEnum == Constant.SearchStateEnum.NICKNAME_EMAIL ? 2 : 3));
        if (searchType == 3) ToastUtils.showShort("请输入搜索内容");
        else presenter.requestStrData(0, Interface.INTERFACE_ACCOUNT, CacheMode.NO_CACHE,
                new HttpParams("RequestMethod", "SearchUser"),
                new HttpParams("SearchType", String.valueOf(searchType)),
                new HttpParams("UniqueId", uniqueId),
                new HttpParams("Search", s));
    }

    @Override
    public void showOnStart(Request request, int requestId) {
        loadingDogDialog.show();
    }

    @Override
    public void showOnSuccess(int requestId, String responseBody, Response response) {
        super.showOnSuccess(requestId, responseBody, response);
        SearchUserRes searchUserRes = gson.fromJson(responseBody, SearchUserRes.class);
        if (searchUserRes.isResultStatus()) {
            resultListDataBeans = searchUserRes.getResultListData();
            searchUserListAdapter.setNewData(resultListDataBeans);
        }
    }

    @Override
    public void showOnFinish(int requestId) {
        super.showOnFinish(requestId);
        loadingDogDialog.dismiss();
    }

    @Override
    public void onTitleClick(int btnType) {
        if (btnType == 1) finish();
    }
}
